import { save, pages, LoadFrom, Delete, CategoryPage, CloseJob } from '@/api/job/job'
// import { DepartmentTree } from '@/api/systems/systemDepartment'

const state = {
  controllerName: 'job',
  dataList: {
    languageList: ['普通话', '英语', '日语', '法语', '粤语', '其他'],
    jobStatusList: [{ 'value': '0', 'label': '关闭' }, { 'value': '1', 'label': '招聘中' }],
    salaryList: ['4k及以下', '4k-6k', '6k-8k', '8k-10k', '10k-12k', '12k及以上'],
    sexList: ['不限', '男', '女'],
    listLoading: false,
    list: [],
    departmentTree: [],
    defaultProps: {
      children: 'departmentList',
      key: 'id',
      label: 'name'
    },
    searchForm: {
      LoginName: '',
      pageSize: 10,
      pageIndex: 1,
      dataCount: 0,
      pageCount: 0
    }
  },
  form: {
    formState: false,
    formDepartmentState: false,
    model: {},
    ResetPwdFormState: false,
    ResetPwdModel: {},
    CategoryOneList: [], // 一级分类
    CategoryTwoList: [], // 二级分类
    CategoryThreeList: [], // 三级分类
    selectedProvince: [], // 选择的省市区
    TextToCode: [], // 选择的省市区
    checkList: [],
    closeState: false,
    closemodel: { type: '0', Reason: '' }

  }
}
const mutations = {
  pageSizeChange(state, par) {
    state.dataList.searchForm.pageSize = par
    this.dispatch(`job/${state.controllerName}/findList`)
  },
  currentPageChange(state, par) {
    state.dataList.searchForm.pageIndex = par
    this.dispatch(`job/${state.controllerName}/findList`)
  }
}

const actions = {
  loadNewForm(context) {
    var _state = context.state
    _state.form.model = {}
    _state.form.formState = true
  },
  // 加载表单
  loadForm(context, par) {
    var _state = context.state
    console.log('加载表单参数', par)
    LoadFrom({ ID: par })
      .then(response => {
        const { Data } = response
        console.log(Data)
        _state.form.model = Data

        if (_state.form.model.CategoryOneId !== '00000000-0000-0000-0000-000000000000') {
          context.dispatch('getCategoryPage', { parentId: _state.form.model.CategoryOneId, level: 2, cls: false })
        }
        if (_state.form.model.CategoryTwoId !== '00000000-0000-0000-0000-000000000000') {
          context.dispatch('getCategoryPage', { parentId: _state.form.model.CategoryTwoId, level: 3 })
        }
        try {
          // 设置默认省市区
          _state.form.selectedProvince = [_state.form.TextToCode[_state.form.model.Province].code, _state.form.TextToCode[_state.form.model.Province][_state.form.model.City].code, _state.form.TextToCode[_state.form.model.Province][_state.form.model.City][_state.form.model.District].code]
        } catch (ee) {
          console.log('_state.form.selectedProvince.ee', ee)
          _state.form.selectedProvince = []
        }

        console.log('_state.form.selectedProvince', _state.form.selectedProvince)
        // 语言
        if (_state.form.model.Language !== '' && _state.form.model.Language != null) {
          _state.form.checkList = _state.form.model.Language.split(',')
        }
        // 最小年龄默认为0 最大年龄默认为空
        if (_state.form.model.MinAge === 0 || _state.form.model.MinAge === '') {
          _state.form.model.MinAge = 0
        }
        if (_state.form.model.MaxAge === 0) {
          _state.form.model.MaxAge = ''
        }
        _state.form.formState = true
      })
      .catch(error => {
        console.log(error)
      })
  },
  loadCloseJob(context, par) {
    var _state = context.state
    _state.form.closemodel.ID = par.ID
    _state.form.closemodel.type = par.Status + ''
    _state.form.closemodel.Reason = par.CloseReason
    _state.form.closeState = true
  },
  // 关闭开启职位
  closeJob(context, par) {
    var _state = context.state
    console.log('关闭开启职位', par)
    if (par.Reason === '' || par.Reason === null) {
      global.tools.msg('请填写原因!', '错误')
      return
    }
    CloseJob(par)
      .then(response => {
        const { Data } = response
        console.log(Data)
        _state.form.resState = Data
        if (_state.form.resState) {
          global.tools.msg('操作成功!', '成功')
          // _state.form.closemodel = {}
          _state.form.closeState = false
          context.dispatch('findList')
        } else {
          global.tools.msg('操作失败!', '错误')
        }
      })
      .catch(error => {
        console.log(error)
      })
  },
  save(context, par) {
    var _state = context.state
    if (par.Salary === '' || par.Salary === null) {
      global.tools.msg('请选择薪资范围!', '错误')
      return
    }
    if (par.MinAge === '') {
      par.MinAge = 0
    }
    if (par.MaxAge === '') {
      par.MaxAge = 0
    }
    par.Language = _state.form.checkList.join(',')
    console.log(par.Language)

    save(par)
      .then(response => {
        const { Data } = response
        console.log(Data)
        global.tools.msg('保存成功!', '成功')
        _state.form.formState = false

        context.dispatch('findList')
      })
      .catch(error => {
        console.log(error)
      })
  },
  findList(context, par) {
    var _state = context.state
    _state.dataList.listLoading = true
    pages(_state.dataList.searchForm)
      .then(res => {
        const { Data } = res
        console.log(Data)
        _state.dataList.list = Data.List
        _state.dataList.searchForm.pageIndex = Data.PageIndex
        _state.dataList.searchForm.pageSize = Data.PageSize
        _state.dataList.searchForm.dataCount = Data.Count
        _state.dataList.searchForm.pageCount = Data.Pages
        _state.dataList.listLoading = false
      })
      .catch(error => {
        console.log(error)
      })
  },
  delete(context, par) {
    console.log(par)
    global.tools.confirm(
      '确定要删除吗？',
      function() {
        Delete({
          id: par
        })
          .then(res => {
            const { Data } = res
            console.log(Data)
            global.tools.msg('删除成功!', '成功')
            context.dispatch('findList')
          })
          .catch(e => {
            console.log(e)
          })
      },
      null,
      '提示'
    )
  },
  getCategoryPage(context, par) {
    var _state = context.state
    var parentId = par.parentId || '00000000-0000-0000-0000-000000000000'
    var level = par.level || 1
    CategoryPage({
      parentId: parentId,
      pageCount: 1000,
      pageIndex: 1
    })
      .then(res => {
        const { Data } = res
        if (level === 1) { // 请求一级分类
          _state.form.CategoryOneList = Data.List
        } else if (level === 2) { // 二级分类
          _state.form.CategoryTwoList = Data.List
          if (par.cls) {
            _state.form.model.CategoryTwoId = '00000000-0000-0000-0000-000000000000'
            _state.form.model.CategoryThreeId = '00000000-0000-0000-0000-000000000000'
            _state.form.CategoryThreeList = []
          }
        } else if (level === 3) { // 二级分类
          _state.form.CategoryThreeList = Data.List
          if (par.cls) {
            _state.form.model.CategoryThreeId = '00000000-0000-0000-0000-000000000000'
          }
        }
      })
      .catch(e => {
        console.log(e)
      })
  },

  handleNodeClick(context, node) {
    var _state = context.state
    _state.form.model.departmentId = node.id
    _state.form.model.departmentName = node.name
    _state.form.formDepartmentState = false
  },
  handleSuccess(context, res) {
    console.log(res)
    if (res.Status === 1) {
      var _state = context.state
      _state.form.model.HeaderImage = res.Data.Url
      console.log('_state.form.model.HeaderImage', _state.form.model.HeaderImage)
    } else {
      global.tools.msg('上传失败，请重新上传！', '提示')
    }
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
